package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.dunhuangshiku.DunHuangShiKuModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/20 22:08
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_EXPLORE, desc = "敦煌石窟探索")
public class DunHuangShiKuExploreCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuExploreReqMsg req = DunHuangShiKuProto.DunHuangShiKuExploreReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        DunHuangShiKuProto.DunHuangShiKuExploreReqMsg.Builder builder = req.toBuilder();
        ret = DunHuangShiKuMgr.canEnterActivitying(player,activityId);
        if(ret != 0){
            return;
        }
        DunHuangShiKuModule dunHuangShiKuModule = player.getModule(DunHuangShiKuModule.class);
        Map<Integer, Long> boxKeyNumMap = dunHuangShiKuModule.baseData.getBoxKeyNumMap();
        for (Map.Entry<Integer, Long> entry : boxKeyNumMap.entrySet()) {
            Integer id = entry.getKey();
            Long num = entry.getValue();
            DunHuangShiKuProto.BoxKeyNumMsg.Builder msg = DunHuangShiKuProto.BoxKeyNumMsg.newBuilder();
            msg.setBoxId(id);
            msg.setNum(num.intValue());
            builder.addBoxKey(msg);
        }
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_DUN_HUANG_SHI_KU_EXPLORE, builder, req.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DunHuangShiKuProto.DunHuangShiKuGetTeamListRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetTeamListRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_EXPLORE, builder);
        }
    }
}
